<template>
  <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
    :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'" v-loading="loading">
    <!--      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">-->
    <el-form-item label="表单名称" prop="name">
      <!--文本框-->
      <el-input v-model="dataForm.name" placeholder="表单名称" clearable></el-input>
    </el-form-item>
    <el-form-item label="业务类型" prop="type">
      <!--文本框-->
      <el-select v-model="dataForm.type" placeholder="请选择业务类型">
        <el-option v-for="template in this.$getDictList('BPM_FORM_TYPE', false)" :key="template.val"
          :label="template.label" :value="template.val"></el-option>
      </el-select>
    </el-form-item>
    <!-- <el-form-item label="打印模板" prop="printTemplate">
      <el-select v-model="dataForm.printTemplateCode" filterable remote reserve-keyword clearable placeholder="请选择打印模板"
        :remote-method="remoteMethod">
        <el-option v-for="item in options" :key="item.templateCode" :label="item.templateName"
          :value="item.templateCode">
        </el-option>
      </el-select>
    </el-form-item> -->
    <el-form-item label="排序" prop="sort">
      <el-input-number v-model="dataForm.sort" :min="0" :max="10"></el-input-number>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注信息" type="textarea" :autosize="{ minRows: 2, maxRows: 4}">
      </el-input>
    </el-form-item>
  </el-form>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      dataForm: {
        id: '',
        name: '',
        type: '',
        sort: '',
        // printTemplateCode: '',
        // printTemplateName: '',
        remark: ''
      },
      options: [],
      loading: false
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.options = []
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        } else {
          // this.remoteMethod('')
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/flowable/bpmFormDef/${this.dataForm.id}`, {
        params: {
          limit: 100
        }
      }).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        // this.remoteMethod('')
      }).catch(() => { })
    },
    // 查询打印模板
    remoteMethod (query) {
      // let params = {
      //   'params': {
      //     'templateCode': query,
      //     'templateName': query
      //   }
      // }
      // this.$http.get('/sys/sysPrintTemplate/list', params).then(({ data: res }) => {
      //   if (res.code !== 0) {
      //     return false
      //   }
      //   this.options = res.data
      // })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/flowable/bpmFormDef', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.$emit('refreshNextData', res)
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
